//让day1~day5和横轴移动
import { Behaviour } from "../engine/Behaviour";
import { getGameObjectById } from "../engine";
import { TextRenderer } from "../engine/TextRenderer";
import { BitmapRenderer } from "../engine/BitmapRenderer";
import { Bill_OK_Click } from "./Bill_OK_Click";
import { Transform } from "../engine/Transform";

let isScaled = 0

    //记录day1~5的初始位置
    //let FormalPositionX:number[]

export class textMove extends Behaviour{

    //每一次进到这个场景/每一次点“下一天”，currentday+1
    currentday:number = 1
    FormalDay:number = 1
    public speed = 1.5
    public isMoved = 1

    yes:boolean = false

    FormalPositionX:number[] = new Array(5) 
    

    onStart(){
       this.aaa()
        
    }
    aaa(){
        setTimeout(() => {
            //console.log("formal:" + this.FormalDay + ";current:" + this.currentday)
            if(this.FormalDay !== this.currentday){
                //console.log("重新计算坐标~~~~~")
                
                this.getFormalPos()
                //重新计算坐标后再调move函数
                this.yes = true
                this.isMoved = 1
                this.FormalDay = this.currentday                
            }else{
                //console.log("formal = current")
            }
            this.aaa()            
        }, 500);
    }

    getFormalPos(){
            //循环得到5个文字的初始位置存入数组
            for(let i = 1; i < 6; i++ ){
                let dayStr = "day" + i
                if(getGameObjectById(dayStr)){
                    this.FormalPositionX[i-1] = getGameObjectById(dayStr).getBehaviour(Transform).x
                }
            }        
    }

    onTick(duringTime: number){
        //得到当前天数
        this.currentday = getGameObjectById('UI_Button_Nextday').getBehaviour(Bill_OK_Click).dayNum
        //console.log("currentday：" + this.currentday)
        //遍历改变位置
        for(let i = 1; i < 6; i++ ){
            let dayStr = "day" + i
            const textTrans = getGameObjectById(dayStr).getBehaviour(Transform)
            if(this.yes){
                this.move(textTrans,this.FormalPositionX[i-1])
                //console.log("进yes里面了")
            }
            
        }
        
    }

    move(trans:Transform, formalx:number){
       
        if(this.isMoved === 1){
            //console.log("移动了！！！！！！！！！！！！！！！")
            trans.x -= this.speed;
            isScaled = 1
            this.scaleText(this.currentday)
        }
        if(trans.x < formalx-160){
            this.isMoved = 0;
            isScaled = 0
            //move结束后yes=false，等待下一次坐标更新
            this.yes = false
        }
    }

    scaleText(day:number){
        let dayTxt = "day" + day
       

        if(isScaled === 1){
            //放大今日文字
            getGameObjectById(dayTxt).getBehaviour(Transform).scaleX += 0.0015
            getGameObjectById(dayTxt).getBehaviour(Transform).x -= 0.06
            getGameObjectById(dayTxt).getBehaviour(Transform).scaleY += 0.0015                
            getGameObjectById(dayTxt).getBehaviour(Transform).y -= 0.06
            //console.log(getGameObjectById(dayTxt).getBehaviour(Transform).scaleX)
            // 缩小今日文字
            let formalDay = day - 1
            if(formalDay > 1){
                let formalDayTxt = "day" + formalDay
                getGameObjectById(formalDayTxt).getBehaviour(Transform).scaleX -= 0.0015
                getGameObjectById(formalDayTxt).getBehaviour(Transform).x += 0.06
                getGameObjectById(formalDayTxt).getBehaviour(Transform).scaleY -= 0.0015                
                getGameObjectById(formalDayTxt).getBehaviour(Transform).y += 0.06
                //console.log(getGameObjectById(formalDayTxt).getBehaviour(Transform).scaleX)
            }
        }
    }

}